//
//  DZZHomeViewController.m
//  DZZTravel
//
//  Created by xalo on 16/7/13.
//  Copyright © 2016年 Eason. All rights reserved.
//

#import "DZZHomeViewController.h"


@interface DZZHomeViewController ()<UITableViewDataSource,UITableViewDelegate,passValue>

//存放轮播图的数组
@property (nonatomic,strong)NSMutableArray *scroviewArray;

//存放btn模型的数组
@property(nonatomic,strong)NSMutableArray *btnArray;

//存放推荐的模型的数组
@property (nonatomic,strong)NSMutableArray *recomAry;
@property (strong, nonatomic) IBOutlet UIView *firstSectionView;

//存放cell的高度
//头部view
@property (nonatomic,strong)UIView *headView;

@property (nonatomic,assign)NSInteger hi;

@property (weak, nonatomic) IBOutlet UITableView *tableView;
//装周边模型的数组
@property (nonatomic,strong)NSMutableArray *aroundArray;

//装国内热门的数组
@property (nonatomic,strong)NSMutableArray *hotArray;
//转换点击按钮的数组
@property (nonatomic,strong)NSMutableArray *changeArray;

@property (nonatomic,strong)UIView *gView;

@property (nonatomic,strong)UIView *secView;
//section上的btn
@property (nonatomic,strong)UIButton *btn1;
@property (nonatomic,strong)UIButton *btn2;
@property (nonatomic,strong)UIView *followView;

@property (nonatomic,strong)NSString *string;
@property (nonatomic,strong)NSNumber *cityID;



@end

@implementation DZZHomeViewController



- (NSMutableArray *)changeArray{
    if (!_changeArray) {
        _changeArray = [NSMutableArray array];
    }
    return _changeArray;
}

- (NSMutableArray *)hotArray{
    if (!_hotArray) {
        _hotArray = [NSMutableArray array];
    }
    return _hotArray;
}
- (NSMutableArray *)recomAry{
    if (!_recomAry) {
        _recomAry = [NSMutableArray array];
    }
    return _recomAry;
}

- (NSMutableArray *)scroviewArray{
    if (!_scroviewArray) {
        _scroviewArray = [NSMutableArray array];
    }
    return _scroviewArray;
}

- (NSMutableArray *)btnArray{
    if (!_btnArray) {
        _btnArray = [NSMutableArray array];
    }
    return _btnArray;
}
- (NSMutableArray *)aroundArray{
    if (!_aroundArray) {
        _aroundArray = [NSMutableArray array];
    }
    return _aroundArray;
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];

    [self loadData];
    [self loadRecomData];
    [self loadHotdata];
    
    [self loadAroundData];
    //
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithTitle:[NSString stringWithFormat:@"我在%@",self.string] style:UIBarButtonItemStylePlain target:self action:@selector(leftAction)];
    [self setHotAndAroundPlay];
    
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.automaticallyAdjustsScrollViewInsets = NO;
    
    
    self.cityID = @610100;
    self.string = @"西安";
    [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"cell"];
    [self.tableView registerNib:[UINib nibWithNibName:@"DZZFiveButtonCell" bundle:nil] forCellReuseIdentifier:@"fivebtn"];
    self.tableView.contentInset = UIEdgeInsetsMake(0, 0, 44, 0);
    
    
    
    
    
//    注册cell
    [self.tableView registerNib:[UINib nibWithNibName:@"DZZAroundCell" bundle:nil] forCellReuseIdentifier:@"DZZAroundCell"];

    [self.tableView registerNib:[UINib nibWithNibName:@"DZZCollectionCell" bundle:nil] forCellReuseIdentifier:@"DZZCollectionCell"];
    
    
}
//设置国内热门和周边游
- (void)setHotAndAroundPlay{
    self.secView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, self.view.width, 50)];
    self.secView.backgroundColor = [UIColor lightGrayColor];
    self.btn1 = [[UIButton alloc]initWithFrame:CGRectMake(self.view.width/3-100, 10, 60, 30)];
    [self.btn1 setTitle:@"周边游" forState:UIControlStateNormal];
    self.followView = [[UIView alloc]initWithFrame:CGRectMake(self.btn1.x, self.btn1.y+self.btn1.height-1, 60, 2)];
    self.followView.backgroundColor = [UIColor redColor];
    self.btn2 = [[UIButton alloc]initWithFrame:CGRectMake(self.btn1.x+self.btn1.width+60, 10, 80, 30)];
    
    [self.btn2 setTitle:@"国内热门" forState:UIControlStateNormal];
    
    [self.btn1 addTarget:self action:@selector(btn1Action) forControlEvents:UIControlEventTouchUpInside];
    [self.btn2 addTarget:self action:@selector(btn2Action) forControlEvents:UIControlEventTouchUpInside];
    
    
    [self.secView addSubview:self.followView];
    [self.secView addSubview:self.btn1];
    [self.secView addSubview:self.btn2];
}


//加载btn数据
- (void)loadData{


    [RequestManager fectDataWithUrl:[NSString stringWithFormat:@"http://apiphp.yaochufa.com/you/advertiselist/AdList?area_code=%@&channel=AppStore&column=%@&imei=086DA4DD-1FAB-421A-8AC3-3CF9F2718437&lang=app&machineCode=086DA4DD-1FAB-421A-8AC3-3CF9F2718437&machineType=0&system=ios&version=5.2.3",self.cityID,@"bannerSquare%2CbannerRound%2CbannerScroll%2CAPPFind%2CappBanner%2CappSuspend%2CappHeadlines"] parameter:@{} method:@"GET" compelet:^(NSData *data) {
        NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:nil];
        if (self.scroviewArray.count>0) {
            [self.scroviewArray removeAllObjects];
        }
        if (self.btnArray.count>0) {
            [self.btnArray removeAllObjects];
        }
        for (NSDictionary *dic2 in dic[@"content"][0][@"ad"]) {
            DZZScrollViewModel *model = [[DZZScrollViewModel alloc]init];
            [model setValuesForKeysWithDictionary:dic2[@"ct"]];
          
            [self.scroviewArray addObject:model.app_picpath];
        }
        
        for (NSDictionary *dic3  in dic[@"content"][1][@"ad"]) {
            DZZScrollViewModel *model = [[DZZScrollViewModel alloc]init];
            [model setValuesForKeysWithDictionary:dic3[@"ct"]];
            [self.btnArray addObject:model];
            
        }
        
        
        self.headView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, kWidth, kheight/2)];
        
        LTScrollView *sc = [[LTScrollView alloc]initWithFrame:CGRectMake(0, 0, kWidth, kheight/4) imageArray:self.scroviewArray];
        [self.headView addSubview:sc];
       
        int a = 0;
        for (int i = 0; i<self.btnArray.count; i++) {
       
            KLButton *btn = [[KLButton alloc]initWithFrame:CGRectMake(0, sc.height, self.headView.width/5, self.headView.width/5)];
            if (i >= 5) {
                btn.x = (i-5)*self.headView.width/5;
                btn.y = sc.height+self.headView.width/5;
            }else{
                btn.x = i*self.headView.width/5;
            }
            [btn setCateModel:self.btnArray[i]];
//            给btn添加点击事件
            btn.tag = a;
            a++;
            [btn addTarget:self action:@selector(btnAction:) forControlEvents:UIControlEventTouchUpInside];
            
            
            

            [self.headView addSubview:btn];
           }
            if (self.btnArray.count>5) {
                self.hi = sc.height+2*self.headView.width/5;
            }else{
                self.hi = sc.height+self.headView.width/5;
               
        }
         self.headView.height = self.hi;
    } upDateUI:^{
        self.tableView.tableHeaderView = self.headView;
        
    }];
}

    
//加载推荐
- (void)loadRecomData{
//    @"http://apiphp.yaochufa.com/portal/scenic/ScenicIndex?imei=086DA4DD-1FAB-421A-8AC3-3CF9F2718437&system=ios&cityCode=110100&channel=AppStore&lang=app&version=5.2.3"
    [RequestManager fectDataWithUrl:[NSString stringWithFormat:@"http://apiphp.yaochufa.com/portal/scenic/ScenicIndex?imei=086DA4DD-1FAB-421A-8AC3-3CF9F2718437&system=ios&cityCode=%@&channel=AppStore&lang=app&version=5.2.3",self.cityID] parameter:@{} method:@"GET" compelet:^(NSData *data) {
      
        NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:nil];
        if (self.recomAry.count > 0) {
            
            [self.recomAry removeAllObjects];
        }
        for (NSDictionary *dic2 in dic[@"content"][@"scenicData"]) {
            DZZRecommedModel *model = [[DZZRecommedModel alloc]init];
            [model setValuesForKeysWithDictionary:dic2];
            [self.recomAry addObject:model];
        }

    } upDateUI:^{
        
    }];
    
}
//加载周边数据
- (void)loadAroundData{
//    @"http://apiphp.yaochufa.com/playpoint/qualityTabs?system=ios&channel=AppStore&currentCityCode=610100&longitude=108.90690590045&cityCode=110100&imei=086DA4DD-1FAB-421A-8AC3-3CF9F2718437&lang=app&latitude=34.229654187594&version=5.2.3"
    [RequestManager fectDataWithUrl:[NSString stringWithFormat:@"http://apiphp.yaochufa.com/playpoint/qualityTabs?system=ios&channel=AppStore&currentCityCode=610100&longitude=108.90690590045&cityCode=%@&imei=086DA4DD-1FAB-421A-8AC3-3CF9F2718437&lang=app&latitude=34.229654187594&version=5.2.3",self.cityID] parameter:@{} method:@"GET" compelet:^(NSData *data) {
        if (self.aroundArray.count>0) {
            [self.aroundArray removeAllObjects];
        }
        NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:nil];
        for (NSDictionary *dic2 in dic[@"content"][0][@"content"]) {
            DZZCityAroundModel *around = [[DZZCityAroundModel alloc]init];
            [around setValuesForKeysWithDictionary:dic2];
            [self.aroundArray addObject:around];
          
        }
        self.changeArray = self.aroundArray;
    } upDateUI:^{
        [self.tableView reloadData];
    }];
}
//加载国内热门
- (void)loadHotdata{
    
    [RequestManager fectDataWithUrl:@"http://apiphp.yaochufa.com/playpoint/quality?system=ios&version=5.2.3&channel=AppStore&deviceToken=349d559ca4fa4a8cac02854249444fdd57c55fdfc3a41e8283a3070fba685e68&longitude=108.90690209706&pageIndex=2&tabType=3&latitude=34.229635196071&cityCode=610100&currentCityCode=610100&imei=086DA4DD-1FAB-421A-8AC3-3CF9F2718437&lang=app" parameter:@{} method:@"GET" compelet:^(NSData *data) {
        
        NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:nil];
        for (NSDictionary *dic2 in dic[@"content"]) {
            DZZCityAroundModel *model = [[DZZCityAroundModel alloc]init];
            [model setValuesForKeysWithDictionary:dic2];
            [self.hotArray addObject:model];
          
        
        }
        
    } upDateUI:^{

        
    }];
}

//导航左边的点击事件
- (void)leftAction{
    DZZAddressViewController *address = [[DZZAddressViewController alloc]init];
    
    address.delegate = self;
    
    [self.navigationController pushViewController:address animated:YES];
    
    
    
}


#pragma mark -------------------TableView协议-------------------------
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if (section == 0) {
        return 1;
    }else{
        return self.changeArray.count;
    }
    
}
//返回section的类型
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    if (section == 0) {
        return self.firstSectionView;
    }else{
    return self.secView;
    }
    }
//返回section 的高度
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
        
        return 50;
    
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 2;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section == 1) {
        DZZAroundCell *cell  = [tableView dequeueReusableCellWithIdentifier:@"DZZAroundCell" forIndexPath:indexPath];
        if (self.changeArray.count>0) {
            
        
        [cell setCellForModel:self.changeArray[indexPath.row]];
        }
        return cell;
    }
    else{
    DZZCollectionCell *cell = [tableView dequeueReusableCellWithIdentifier:@"DZZCollectionCell" forIndexPath:indexPath];
//        传过去一个数组
        
        [cell setCellOfArray:self.recomAry];
      
        
        return cell;
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section == 0) {
        
        if (self.recomAry.count>3&&self.recomAry.count<9) {
            return (([UIScreen mainScreen].bounds.size.width-20)/3-20)*2+18;
        }else if (self.recomAry.count == 0){
            return 0;
        }else if (self.recomAry.count<=3) {
        return (([UIScreen mainScreen].bounds.size.width-20)/3-20)*1+13;
        }else{
            return (([UIScreen mainScreen].bounds.size.width-20)/3-20)*3+28;}
    }else{
        return 200;}
    
    
}
//TableViewCell点击事件
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section == 1) {
        
        DZZAroundAndHotController *around = [[DZZAroundAndHotController alloc]initWithNibName:@"DZZAroundAndHotController" bundle:nil];
        
        if (self.changeArray.count>0) {
            
          DZZCityAroundModel *mo =  self.changeArray[indexPath.row] ;
            around.pID = mo.productId;
            
            around.cID = mo.channelLinkId;
            
        }
        
        
        
        
        [self.navigationController pushViewController:around animated:YES];
        
    }
    
    
}


//周边点击事件
- (void)btn1Action{
    self.changeArray = self.aroundArray;
    [self.tableView reloadData];
    [self change:self.btn1];
}

//国内热门点击事件
- (void)btn2Action{
    
    self.changeArray = self.hotArray;
    [self.tableView reloadData];
    
//    偏移量
//    self.tableView setContentOffset:<#(CGPoint)#> animated:<#(BOOL)#>
    [self change:self.btn2];
}
//改变btn的方法
- (void)change:(UIButton *)btn{
    self.btn1.enabled = YES;
    
    self.btn2.enabled = YES;
    btn.enabled = NO;
    
    CGRect oldRect = self.followView.frame;
    oldRect.size.width = btn.width;
    oldRect.origin.x = btn.x;
    [UIView animateWithDuration:0.25 animations:^{
        self.followView.frame = oldRect;
    }];
}

//btn点击事件
- (void)btnAction:(UIButton *)btn{
    
    DZZDetailsViewController *details = [[DZZDetailsViewController alloc]initWithNibName:@"DZZDetailsViewController" bundle:nil];
    details.city = self.string;
//
//        "ct": {
//            "app_picpath": "http://cdn6.jinxidao.com/group1/M00/02/FD/ooYBAFeIn1iAE-DmAAAYFUPthiU773.png",
//            "app_url_site": 4,
//            "app_url": "404",
//            "title": "避暑",
//            "new_app_picpath": "",
//            "adId": 9806,
//            "appToUrl": "yaochufa://property/404"
//    {
//        "ct": {
//            "app_picpath": "http://cdn6.jinxidao.com/group1/M00/02/FB/oYYBAFeIqpiAfMotAAAsb8gyF1I356.png",
//            "app_url_site": 5,
//            "app_url": "68",
//            "title": "门票",
//            "new_app_picpath": "",
//            "adId": 5288,
//            "appToUrl": "yaochufa://tag/68"
//        }
    
      details.model = self.btnArray[btn.tag];
    
    
    
    [self.navigationController pushViewController:details animated:YES];
    
    

    
}



#pragma mark --------------------passValueDelegate协议-----------------------

//实现协议方法

- (void)passValueWithModel:(CityModel *)model{
    
 
    self.string = [NSString stringWithFormat:@"%@",model.cityNameAbbr];
    self.cityID = model.cityCode;
}


@end
